package com.jianzhile.jobs.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jianzhile.jobs.R;
import com.jianzhile.jobs.activities.mine.CompanyDetailActivity;
import com.jianzhile.jobs.activities.mine.MyPubActivity;
import com.jianzhile.jobs.activities.mine.MyResumeActivity;
import com.jianzhile.jobs.activities.mine.MySubjobActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MineFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment implements View.OnClickListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    @Bind(R.id.fragment_mine_myjob_ll)
    public LinearLayout subjobLl;

    @Bind(R.id.fragment_mine_publish_ll)
    public LinearLayout publishLl;

    @Bind(R.id.fragment_mine_resume_ll)
    public LinearLayout resumeLl;

    @Bind(R.id.fragment_mine_company_ll)
    public LinearLayout companyLl;

    private OnFragmentInteractionListener mListener;

    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, view);
        return  view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        initListener();
    }

    private void initListener(){
        subjobLl.setOnClickListener(this);
        publishLl.setOnClickListener(this);
        resumeLl.setOnClickListener(this);
        companyLl.setOnClickListener(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fragment_mine_myjob_ll:
                Intent jIntent = new Intent(getActivity(), MySubjobActivity.class);
                startActivity(jIntent);
                break;
            case R.id.fragment_mine_publish_ll:
                Intent pIntent = new Intent(getActivity(), MyPubActivity.class);
                startActivity(pIntent);
                break;
            case R.id.fragment_mine_resume_ll:
                Intent rIntent = new Intent(getActivity(), MyResumeActivity.class);
                startActivity(rIntent);
                break;
            case R.id.fragment_mine_company_ll:
                Intent cIntent = new Intent(getActivity(), CompanyDetailActivity.class);
                startActivity(cIntent);
                break;
        }
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);

        void initToolBar(int tab);
    }
}
